package com.regulareport.web.task;

import com.regulareport.common.core.domain.entity.ReportPlan;
import com.regulareport.common.core.domain.entity.ReportTask;
import com.regulareport.common.enums.*;
import com.regulareport.common.utils.SecurityUtils;
import com.regulareport.form.mapper.ReportPlanMapper;
import com.regulareport.form.mapper.ReportTaskMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Component
public class DeliveryTask {
//    private final ReportPlanMapper reportPlanMapper;
//    private final ReportTaskMapper reportTaskMapper;
//    private static final Logger log = LoggerFactory.getLogger(DeliveryTask.class);
//
//    public DeliveryTask(ReportPlanMapper reportPlanMapper, ReportTaskMapper reportTaskMapper) {
//        this.reportPlanMapper = reportPlanMapper;
//        this.reportTaskMapper = reportTaskMapper;
//    }
//
//    // 每天凌晨 2 点执行（cron 表达式：秒 分 时 日 月 周）
////    @Scheduled(cron = "0 0 1 * * ?")
//    public void delivery() {
//        log.info("开始下发任务...");
//        ReportPlan reportPlan = new ReportPlan();
//        reportPlan.setEnable(EnableStatus.OK.getCode());
//        reportPlan.setDelFlag(DelFlag.NORMAL.getCode());
//        reportPlan.setDeliveryType(DeliveryType.AUTO.getCode());
//        reportPlan.setDeliveryStatus(DeliveryStatus.DELIVERY.getCode());
//        List<ReportPlan> reportPlans = reportPlanMapper.selectDeliveryPlan(reportPlan);
//        List<ReportPlan> deliveryPlan = new ArrayList<>();
//        LocalDate currentDate = LocalDate.now();
//        LocalDate beforeYear = currentDate.plusYears(-1L);
//        LocalDate iteYear = LocalDate.of(beforeYear.getYear(), 9, 1);
//        for (int i = 0; i < 19; i++) {
//            LocalDate handleM = iteYear.plusMonths(i);
//            for (ReportPlan plan : reportPlans) {
//                plan.setUpdateBy("SYSTEM");
//                int deadLine = Integer.parseInt(plan.getDeadLine());
//                int biasDay = Integer.parseInt(plan.getBiasDay());
//                LocalDate handleD = LocalDate.of(handleM.getYear(), handleM.getMonth(), deadLine);
//                LocalDate compareD = handleD.plusDays(-biasDay);
//                if (FrequencyType.MONTH.getCode().equals(plan.getFrequency())) {
//                    if (currentDate.isEqual(compareD) || currentDate.isAfter(compareD)) {
//                        deliveryPlan.add(plan);
//                    }
//                } else if (FrequencyType.QUARTER.getCode().equals(plan.getFrequency())) {
//                    if (compareD.getMonthValue() == 3 || compareD.getMonthValue() == 9 || compareD.getMonthValue() == 6 || compareD.getMonthValue() == 12) {
//                        if (currentDate.isEqual(compareD) || currentDate.isAfter(compareD)) {
//                            deliveryPlan.add(plan);
//                        }
//                    }
//                } else if (FrequencyType.YEAR.getCode().equals(plan.getFrequency())) {
////                deliveryPlan.add(plan);
//                }
//            }
//        }
//
//        for (ReportPlan plan : deliveryPlan) {
//            ReportTask reportTask = new ReportTask();
//            reportTask.setPlanId(reportPlan.getPlanId());
//            reportTask.setTaskStatus(TaskStatus.DELIVERED.getCode());
//            reportTask.setTaskType(TaskType.AUTO.getCode());
//            reportTask.setCreateBy(SecurityUtils.getLoginUser().getUsername());
//            reportPlanMapper.deliveryByID(plan);
//            reportTaskMapper.deliveryByPlanID(reportTask);
//        }
//        log.info("下发任务成功！");
//    }
}
